package bookshop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bookshop.dao.ReceiveAddrDao;
import bookshop.domain.Category;
import bookshop.domain.ReceiveAddr;
import bookshop.utils.DBUtils;

public class ReceiveAddrDaoJDBCImpl implements ReceiveAddrDao {

	private static final String LOAD_ALL_CUSTID = "select * from receiver_info where cust_id=?";
	private static final String ADD_RECEIVER_ADDR = "insert into receiver_info(receiver,receiver_addr,receiver_phone,zip_code,receiver_status,cust_id) values(?,?,?,?,?,?)";
    private static final String GET_RECEIVE_ADDR_BYID = "select * from receiver_info where receiver_id=?";
	private static final String UPDATE_RECEIVE_ADDR = "update receiver_info set receiver=?,receiver_addr=?,receiver_phone=?,zip_code=?,receiver_status=? where receiver_id=?";
	
	
	public List<ReceiveAddr> GetAllReceiverAddrByCustId(String custId) {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<ReceiveAddr> ras = null;
		try {
			ps = ct.prepareStatement(LOAD_ALL_CUSTID);
			ps.setString(1, custId);
			rs = ps.executeQuery();
			while (rs.next()) {
				if (ras == null)
					ras = new ArrayList<ReceiveAddr>();
				ReceiveAddr ra = new ReceiveAddr();

				ra.setReceiverId(rs.getInt(1));
				ra.setReceiver(rs.getString(2));
				ra.setReceiverAddr(rs.getString(3));
				ra.setReceiverPhone(rs.getString(4));
				ra.setZipCode(rs.getInt(5));
				ra.setReceiverStatus(rs.getString(6));
				ra.setCustId(rs.getString(7));

				ras.add(ra);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, rs);
		}

		return ras;

	}

	public void AddReceiverAddr(ReceiveAddr ra) {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		try {
			ps = ct.prepareStatement(ADD_RECEIVER_ADDR);
			ps.setString(1, ra.getReceiver());
			ps.setString(2, ra.getReceiverAddr());
			ps.setString(3, ra.getReceiverPhone());
			ps.setInt(4, ra.getZipCode());
			ps.setString(5, ra.getReceiverStatus());
			ps.setString(6, ra.getCustId());

			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, null);
		}

	}

	public ReceiveAddr GetReceiverAddrById(int receiveId) {
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ReceiveAddr ra = null;
		try {
			ps = ct.prepareStatement(GET_RECEIVE_ADDR_BYID);
			ps.setInt(1, receiveId);
			rs = ps.executeQuery();
			if (rs.next()) {
				ra = new ReceiveAddr();

				ra.setReceiverId(rs.getInt(1));
				ra.setReceiver(rs.getString(2));
				ra.setReceiverAddr(rs.getString(3));
				ra.setReceiverPhone(rs.getString(4));
				ra.setZipCode(rs.getInt(5));
				ra.setReceiverStatus(rs.getString(6));
				ra.setCustId(rs.getString(7));


			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, rs);
		}

		return ra;

	}

    public void UpdateReceiveAddr(ReceiveAddr ra){
    	Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		try {
			ps = ct.prepareStatement(UPDATE_RECEIVE_ADDR);
			ps.setString(1, ra.getReceiver());
			ps.setString(2, ra.getReceiverAddr());
			ps.setString(3, ra.getReceiverPhone());
			ps.setInt(4, ra.getZipCode());
			ps.setString(5, ra.getReceiverStatus());
			ps.setInt(6, ra.getReceiverId());

			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, null);
		}
    }
}
